package com.kinon.ycjshop.view;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.kinon.ycjshop.R;
import com.kinon.ycjshop.data.UPacketFactory;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import sunmi.ds.DSKernel;
import sunmi.ds.callback.IConnectionCallback;
import sunmi.ds.callback.IReceiveCallback;
import sunmi.ds.data.DSData;
import sunmi.ds.data.DSFile;
import sunmi.ds.data.DSFiles;
import sunmi.ds.data.DataPacket;

/**
 * 晚餐取餐成功
 */

public class WancanpayresultActivity extends Activity {

    private static final String SHOW_IMG_LIST_ID = "SHOW_IMG_LIST_ID";
    private static final String WELCOME_IMG_ID2 = "WELCOME_IMG_ID2";
    private final static String SHOW_VIDEO_LIST_DATA_ID = "SHOW_VIDEO_LIST_DATA_ID";
    private final static String SHOW_IMGS_LIST_DATA_ID = "SHOW_IMGS_LIST_DATA_ID";
    String id;
    String token;
    String balance;
    String koukuan;
    long taskId;
    String filePath = Environment.getExternalStorageDirectory().getPath() + "/dcim/chenggong.png";
    String pathBig = Environment.getExternalStorageDirectory().getPath() + "/dcim/chenggong.png";
    DSKernel mDSKernel;// SDK核心类
    DataPacket dsPacket;
    long fileId;
    private TextView tv_jine;
    private TextView tv_fanhui;
    private EditText et_pay;
    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            try {
                this.update();
            } catch (IOException e) {
                e.printStackTrace();
            }
            handler.postDelayed(this, 1000);// 1妙关闭
        }

        void update() throws IOException {
            finish();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wancanpayresult);
        initRes();
        Show();
    }

    private void Show() {
        DSKernel mDSKernel = DSKernel.newInstance();
        mDSKernel.init(getApplicationContext(), new IConnectionCallback() {
            @Override
            public void onDisConnect() {

            }

            @Override
            public void onConnected(ConnState state) {

            }
        });
        mDSKernel.addReceiveCallback(new IReceiveCallback() {
            @Override
            public void onReceiveData(DSData data) {

            }

            @Override
            public void onReceiveFile(DSFile file) {

            }

            @Override
            public void onReceiveFiles(DSFiles files) {

            }

            @Override
            public void onReceiveCMD(DSData cmd) {

            }
        });
        JSONObject json = new JSONObject();
        try {
            json.put("title", "");
            json.put("content", "取餐成功！");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        DataPacket packet = UPacketFactory.buildShowText(DSKernel.getDSDPackageName(), json.toString(), null);
        mDSKernel.sendData(packet);
    }

    private void initRes() {
        SharedPreferences sp = getSharedPreferences("spReg",
                Context.MODE_PRIVATE);
        token = sp.getString("data", "");
        tv_jine = (TextView) findViewById(R.id.tv_jine);
        balance = getIntent().getStringExtra("balance");
        koukuan = getIntent().getStringExtra("koukuan");
        tv_fanhui = (TextView) findViewById(R.id.tv_fanhui);
        tv_fanhui.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        handler.postDelayed(runnable, 1000);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(runnable);
    }
}
